<?php
declare (strict_types=1);

namespace app\admin\controller;

use addons\BuilderPlus\components\Button;
use addons\BuilderPlus\components\Column;
use addons\BuilderPlus\components\Search;
use addons\BuilderPlus\facade\Table;
use app\common\model\Assets;

class Account extends Base
{
    public function getOptions(): array
    {
        return [
            'pk' => 'id',
            'model' => \app\common\model\Account::class,
            'with' => ['user', 'take'],
            'memory_limit' => '1024M'
        ];
    }

    //账户流水
    public function index(): string
    {
        return $this->fetch();
    }

    //奖金明细
    public function bonus(): string
    {
        return $this->fetch(['is_bonus', '=', 1]);
    }

    //后台充值明细
    public function recharge(): string
    {
        return $this->fetch(['is_admin', '=', 1]);
    }

    public function fetch($extra_map = []): string
    {
        $action = $this->request->action();
        return Table::init($this)
            ->setCheckbox(false)
            ->addColumns([
                ['id', 'ID', fn(Column $col) => $col->width(80)],
                ['uid', '相关会员', fn(Column $col) => $col->templet('#_userTpl')->minWidth(240)],
                ['type', '账户'],
                $action == 'index' ? ['front', '前余额'] : [],
                ['amount', '金额', fn(Column $col) => $col->friendly()],
                $action == 'index' ? ['after', '后余额'] : [],
                $action != 'recharge' ? ['source', '来源会员', fn(Column $col) => $col->minWidth(240)->templet('#_takeUserTpl')] : [],
                $action == 'recharge' ? ['msg', '备注'] : [],
                $action == 'recharge' ? ['operator', '操作人'] : [],
                ['intro', '说明'],
                ['create_time', '时间', fn(Column $col) => $col->width(150)]
            ])
            ->addTopButtons($action == 'recharge' ? [
                'add' => fn(Button $btn) => $btn->title('充值')
                    ->icon('layui-icon-util')
                    ->url(url('user/recharge')->build())
            ] : [])
            ->addSearch([
                ['create_time', '时间', fn(Search $search) => $search->type('date')],
                ['type', '账户类型', fn(Search $search) => $search->type('select')->optionValue(Assets::getFields(3))],
                ['uid', '会员编号/昵称/手机号', fn(Search $search) => $search->relation('\app\common\model\User', 'code|nickname|mobile,id')->condition('Like')],
                $action != 'recharge' ? ['source', '来源会员编号/昵称/手机号', fn(Search $search) => $search->relation('\app\common\model\User', 'code|nickname|mobile,id')->condition('Like')] : '',
                $action == 'bonus' ? ['category', '奖金类型', fn(Search $search) => $search->type('select')->optionValue(getBonusTitle(1))] : []
            ])
            ->specifyQuery($extra_map ? [$extra_map] : [])
            ->enableExport()
            ->fetch();
    }
}